package com.cocostudio.erpapp.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.cocostudio.erpapp.R;

public class CategorizedCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {
    private static final int TYPE_HEADER = 1;
    private static final int TYPE_NORMAL = 0;
    private static final int TYPE_COUNT = 2;

    private ArrayList<String> categories;

    private int[] usedSectionNumbers;

    private SparseIntArray sectionToOffset;
    private SparseIntArray sectionToPosition;
    private LayoutInflater layoutInflater;

    public CategorizedCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags, String categoryColName) {
        super(context, layout, c, from, to, flags);
        layoutInflater = LayoutInflater.from(context);
        categories = new ArrayList<String>();
        sectionToPosition = new SparseIntArray();

        int savedCursorPosition = c.getPosition();
        int i = 0;
        while (c.moveToNext()) {
            String category = c.getString(c.getColumnIndexOrThrow(categoryColName));
            if (!categories.contains(category)) {
                categories.add(category);
            }
            int index = categories.indexOf(category);
            if (sectionToPosition.indexOfKey(index) < 0) {
                sectionToPosition.put(index, i);
            }
            i++;
        }
        c.moveToPosition(savedCursorPosition);

        sectionToOffset = new SparseIntArray();
        int size = sectionToPosition.size();
        usedSectionNumbers = new int[size];
        for (i = 0; i < size; i++) {
            int section = sectionToPosition.keyAt(i);
            sectionToOffset.put(section, i);
            usedSectionNumbers[i] = section;
        }

        for (i = 0; i < size; i++) {
            int section = sectionToPosition.keyAt(i);
            sectionToPosition.put(section, sectionToPosition.get(section) + sectionToOffset.get(section));
        }
    }

    @Override
    public int getCount() {
        if (super.getCount() != 0) {
            return super.getCount() + categories.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (getItemViewType(position) == TYPE_NORMAL) {// we define this
                                                       // function later
            return super.getItem(position - sectionToOffset.get(getSectionForPosition(position)) - 1);
        }

        return null;
    }

    @Override
    public int getPositionForSection(int section) {
        return sectionToPosition.get(section);
    }

    @Override
    public int getSectionForPosition(int position) {
        int i = 0;
        int maxLength = usedSectionNumbers.length;

        while (i < maxLength && position >= sectionToPosition.get(usedSectionNumbers[i])) {
            i++;
        }
        return usedSectionNumbers[i - 1];
    }

    @Override
    public Object[] getSections() {
        return categories.toArray();
    }

    // nothing much to this: headers have positions that the sectionIndexer
    // manages.
    @Override
    public int getItemViewType(int position) {
        if (position == getPositionForSection(getSectionForPosition(position))) {
            return TYPE_HEADER;
        }
        return TYPE_NORMAL;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    // return the header view, if it's in a section header position
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int type = getItemViewType(position);
        if (type == TYPE_HEADER) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.header_category, parent, false);
            }
            ((TextView) convertView.findViewById(R.id.category)).setText((String) getSections()[getSectionForPosition(position)]);
            return convertView;
        }
        return super.getView(position - sectionToOffset.get(getSectionForPosition(position)) - 1, convertView, parent);
    }

    // these two methods just disable the headers
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        if (getItemViewType(position) == TYPE_HEADER) {
            return false;
        }
        return true;
    }
}
